package com.five.api.controller;


import com.five.api.service.CourseCommentService;
import com.five.controller.BaseController;
import com.five.entity.CourseComment;
import com.five.entity.User;
import com.five.vo.BaseResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.httpclient.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author ：vizi
 * @date ：Created in 2021/6/16 11:23
 */
@Controller
@RequestMapping("courseComment")
@Api(tags = "课程评论业务接口")
@CrossOrigin
public class CourseCommentController extends BaseController {

    @Autowired
    private CourseCommentService courseCommentService;

    @ResponseBody
    @RequestMapping(value = "content",method = RequestMethod.GET)
    @ApiOperation("课程评论接口")
    public BaseResultVO<List<CourseComment>> selectCourseContent(@ApiParam("课程id") @RequestParam("courseId") Integer courseId){

        List<CourseComment> courseComments = courseCommentService.selectCourseContent(courseId);

        return successResp(courseComments);
    }

    @ResponseBody
    @RequestMapping(value = "insertComment",method = RequestMethod.GET)
    @ApiOperation("提交评论接口")
    public BaseResultVO insertCourseComment(String content,Long courseId,String username){

        System.out.println(content);
        System.out.println(courseId);
        System.out.println(username);

        User user = new User();
        if (username != null){
            user = courseCommentService.selUserIdByUserName(username);
        }
        System.out.println(user);
        Long userId = user.getId();

        int i = courseCommentService.insertCourseComment(content, courseId, userId);
        System.out.println(i);
        if (i != 1){
            return errorResp("评论失败");
        }
        System.out.println(successRespWithMessage("评论成功"));
        return successRespWithMessage("评论成功");
    }
}
